﻿
Imports System.Windows.Forms
Imports System.Text
Imports System.IO

Public Class DlgSettings

    'Color presets
    'Preset #1
    Dim Preset11 As Color = Color.FromArgb(0, 128, 128)
    Dim Preset12 As Color = Color.FromArgb(192, 192, 192)
    'Preset #2
    Dim Preset21 As Color = Color.FromArgb(191, 144, 0)
    Dim Preset22 As Color = Color.FromArgb(255, 242, 204)
    'Preset #3
    Dim Preset31 As Color = Color.FromArgb(152, 138, 181)
    Dim Preset32 As Color = Color.FromArgb(192, 192, 192)
    'Preset #4
    Dim Preset41 As Color = Color.FromArgb(0, 191, 255)
    Dim Preset42 As Color = Color.FromArgb(192, 192, 192)
    'Preset #5
    Dim Preset51 As Color = Color.FromArgb(50, 100, 100)
    Dim Preset52 As Color = Color.FromArgb(255, 255, 255)

    Dim FullNames As New Dictionary(Of Integer, String)
    Dim Names As New Dictionary(Of Integer, String)
    Dim Folder As String
    Dim BangbezhFile As String 'The Bangbezh INI file in which sound file names are loaded from.
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim OptionsINI As New Ini   'Make a new ini file in memory for settings
        Dim Restart As Boolean = False 'Should Bangbezh be restarted?

        Try
            OptionsINI.Load(Application.StartupPath & "\Data\Options.Bbj") 'Load settings

            'Change the colors
            Color1 = PictColor1.BackColor
            Color2 = PictColor2.BackColor
            FrmMain.ChangeTextColor(Color1, Color2)
            'If the user have changed the city, then Bangbezh must be restarted

            If CboCity.SelectedIndex = City Then
            Else
                City = CboCity.SelectedIndex
                Restart = True
            End If

            'Save settings
            OptionsINI.SetNumberField("Options", "City", CboCity.SelectedIndex)

            OptionsINI.SetNumberField("Options", "R1", PictColor1.BackColor.R)
            OptionsINI.SetNumberField("Options", "G1", PictColor1.BackColor.G)
            OptionsINI.SetNumberField("Options", "B1", PictColor1.BackColor.B)

            OptionsINI.SetNumberField("Options", "R2", PictColor2.BackColor.R)
            OptionsINI.SetNumberField("Options", "G2", PictColor2.BackColor.G)
            OptionsINI.SetNumberField("Options", "B2", PictColor2.BackColor.B)

            OptionsINI.SetNumberField("Options", "Volume", Volume)

            OptionsINI.SetStringField("Options", "Bangbezh", BangbezhFile)
            'Save the INI file
            OptionsINI.Save(Application.StartupPath & "\Data\Options.Bbj")

            'If Restart was needed, then restart.
            If Restart = True Then
                Process.Start(Application.StartupPath & "\Bangbezh.exe")
                FrmMain.Close()
                End
            End If
            Bangbezh.URL = Nothing
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            MsgBox("هەڵەیەک ڕوویدا لەکاتی پاشەکەوت کردنی پەڕگەی ڕێکخستنەکان: " & ex.Message, MsgBoxStyle.Exclamation, "هەڵەی #07")
        End Try
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DlgSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CboBangbezh.Items.Clear()
        Names.Clear()
        FullNames.Clear()
        Dim BangbezhINI As New Ini
        Dim OptionsINI As New Ini 'Make a new INI File for options in memory
        Try

            OptionsINI.Load(Application.StartupPath & "\Data\Options.Bbj") 'Load Data into it
            BangbezhFile = OptionsINI.GetStringField("Options", "Bangbezh")

            BangbezhINI.Load(Application.StartupPath & "\Data\Sounds\" & BangbezhFile)
            Folder = BangbezhINI.GetStringField("Bangbezh", "Folder")

            For Each File In My.Computer.FileSystem.GetFiles(Application.StartupPath & "\Data\Sounds\")
                Dim FileInformation As FileInfo = My.Computer.FileSystem.GetFileInfo(File)
                If FileInformation.Extension.ToUpper = ".BBJ" Then
                    If FullNames.ContainsValue(FileInformation.FullName) Then
                    Else
                        FullNames.Add(FullNames.Count + 1, FileInformation.FullName)
                    End If
                End If
            Next

            For Each Item In FullNames
                Dim INIFile As New Ini
                INIFile.Load(Item.Value)
                If Name.Contains(INIFile.GetStringField("Bangbezh", "Name")) Then
                Else
                    Names.Add(Item.Key, INIFile.GetStringField("Bangbezh", "Name"))
                End If
            Next

            For Each Item In Names
                CboBangbezh.Items.Add(Item.Value)
            Next

            For Each Item In Names
                If Item.Value = BangbezhFile Then
                    CboBangbezh.Text = Names.Item(Item.Key)
                End If
            Next

            'Load settings
            CboCity.Text = Cities(City)
            CboBangbezh.Text = BangbezhINI.GetStringField("Bangbezh", "Name")
            PictColor1.BackColor = Color1
            PictColor2.BackColor = Color2

            TxtR1.Text = PictColor1.BackColor.R.ToString
            TxtR2.Text = PictColor2.BackColor.R.ToString
            TxtG1.Text = PictColor1.BackColor.G.ToString
            TxtG2.Text = PictColor2.BackColor.G.ToString
            TxtB1.Text = PictColor1.BackColor.B.ToString
            TxtB2.Text = PictColor2.BackColor.B.ToString
            Volume = OptionsINI.GetNumberField("Options", "Volume")
            SldrVolume.Value = Volume
            LblVolume.Text = SldrVolume.Value

        Catch ex As Exception
            MsgBox("هەڵەیەک ڕوویدا لەکاتی خوێندنەوەی پەڕگەی ڕێکخستنەکان: " & ex.Message, MsgBoxStyle.Exclamation, "هەڵەی #06")
        End Try
    End Sub

    Private Sub PictColor1_BackColorChanged(sender As Object, e As EventArgs) Handles PictColor1.BackColorChanged
        PicCboCoOp()
    End Sub

    Private Sub PictColor1_Click(sender As Object, e As EventArgs) Handles PictColor1.Click
        'Show color dialog and set pictcolor1.backcolor to the new color
        Dim colordiag As New ColorDialog 'Define new colordialog
        Dim respond As MsgBoxResult = colordiag.ShowDialog 'define a variable for colordialog's result
        If respond = MsgBoxResult.Ok Then 'If the user pressed ok, then continue
            PictColor1.BackColor = colordiag.Color
        End If

    End Sub

    Private Sub PictColor2_BackColorChanged(sender As Object, e As EventArgs) Handles PictColor2.BackColorChanged
        PicCboCoOp()
    End Sub

    Private Sub PictColor2_Click(sender As Object, e As EventArgs) Handles PictColor2.Click
        'Show color dialog and set pictcolor2.backcolor to the new color
        Dim colordiag2 As New ColorDialog
        Dim respond As MsgBoxResult = colordiag2.ShowDialog
        If respond = MsgBoxResult.Ok Then
            PictColor2.BackColor = colordiag2.Color
        End If

    End Sub

    Private Sub Code1_Change(sender As Object, e As EventArgs) Handles TxtR1.Leave, TxtG1.Leave, TxtB1.Leave
        'Getting rid of errors
        If TxtR1.Text = "" Then
            TxtR1.Text = "0"
        End If
        If TxtG1.Text = "" Then
            TxtG1.Text = "0"
        End If
        If TxtB1.Text = "" Then
            TxtB1.Text = "0"
        End If

        Dim R As Integer = CType(TxtR1.Text, Integer)
        Dim G As Integer = CType(TxtG1.Text, Integer)
        Dim B As Integer = CType(TxtB1.Text, Integer)

        PictColor1.BackColor = Color.FromArgb(R, G, B)
    End Sub

    Private Sub Code2_Change(sender As Object, e As EventArgs) Handles TxtR2.Leave, TxtG2.Leave, TxtB2.Leave
        If TxtR2.Text = "" Then
            TxtR2.Text = "0"
        End If
        If TxtG2.Text = "" Then
            TxtG2.Text = "0"
        End If
        If TxtB2.Text = "" Then
            TxtB2.Text = "0"
        End If
        Dim R As Integer = CType(TxtR2.Text, Integer)
        Dim G As Integer = CType(TxtG2.Text, Integer)
        Dim B As Integer = CType(TxtB2.Text, Integer)
        PictColor2.BackColor = Color.FromArgb(R, G, B)
    End Sub

    Private Sub SldrVolume_Scroll(sender As Object, e As EventArgs) Handles SldrVolume.Scroll
        LblVolume.Text = SldrVolume.Value
        Volume = SldrVolume.Value
        Bangbezh.settings.volume = Volume
    End Sub

    Private Sub CboPreset_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboPreset.SelectedIndexChanged
        Select Case CboPreset.SelectedIndex
            Case 0
                PictColor1.BackColor = Preset11
                PictColor2.BackColor = Preset12
            Case 1
                PictColor1.BackColor = Preset21
                PictColor2.BackColor = Preset22
            Case 2
                PictColor1.BackColor = Preset31
                PictColor2.BackColor = Preset32
            Case 3
                PictColor1.BackColor = Preset41
                PictColor2.BackColor = Preset42
            Case 4
                PictColor1.BackColor = Preset51
                PictColor2.BackColor = Preset52
            Case 5

        End Select
    End Sub

    Sub PicCboCoOp()
        TxtR1.Text = PictColor1.BackColor.R
        TxtG1.Text = PictColor1.BackColor.G
        TxtB1.Text = PictColor1.BackColor.B
        TxtR2.Text = PictColor2.BackColor.R
        TxtG2.Text = PictColor2.BackColor.G
        TxtB2.Text = PictColor2.BackColor.B

        If PictColor1.BackColor = Preset11 And PictColor2.BackColor = Preset12 Then
            CboPreset.SelectedIndex = 0
        ElseIf PictColor1.BackColor = Preset21 And PictColor2.BackColor = Preset22 Then
            CboPreset.SelectedIndex = 1
        ElseIf PictColor1.BackColor = Preset31 And PictColor2.BackColor = Preset32 Then
            CboPreset.SelectedIndex = 2
        ElseIf PictColor1.BackColor = Preset41 And PictColor2.BackColor = Preset42 Then
            CboPreset.SelectedIndex = 3
        ElseIf PictColor1.BackColor = Preset51 And PictColor2.BackColor = Preset52 Then
            CboPreset.SelectedIndex = 4
        Else
            CboPreset.SelectedIndex = 5
        End If
    End Sub

    Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
        PictColor1.BackColor = Color.FromArgb(0, 128, 128)
        PictColor2.BackColor = Color.FromArgb(192, 192, 192)
        SldrVolume.Value = 65
    End Sub

    Private Sub SldrVolume_ValueChanged(sender As Object, e As EventArgs) Handles SldrVolume.ValueChanged
        LblVolume.Text = SldrVolume.Value
        Volume = SldrVolume.Value
    End Sub

    Private Sub BtnTest_Click(sender As Object, e As EventArgs) Handles BtnTest.Click
        Try
            Bangbezh.settings.volume = Volume
            Bangbezh.URL = (Application.StartupPath & "\Data\Sounds\" & Folder & "\1.mp3")
        Catch ex As Exception
            MsgBox("هەڵەیەک ڕوویدا لە تاقیکردنەوەی دەنگی بانگبێژی دیاریکراو: " & ex.Message, MsgBoxStyle.Exclamation, "هەڵەی #08")
        End Try
    End Sub

    Private Sub CboBangbezh_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboBangbezh.SelectedIndexChanged
        For Each Item In Names
            If Item.Value = CboBangbezh.Text Then
                Dim Fileinfor As FileInfo = My.Computer.FileSystem.GetFileInfo(FullNames.Item(Item.Key))
                BangbezhFile = Fileinfor.Name
            End If
        Next
        Dim BangbezhINI As New Ini
        Try
            BangbezhINI.Load(Application.StartupPath & "\Data\Sounds\" & BangbezhFile)
            Folder = BangbezhINI.GetStringField("Bangbezh", "Folder")
        Catch ex As Exception
            MsgBox("هەڵەیەک ڕوویدا لەکاتی خوێندنەوەی پەڕگەی ڕێکخستنەکان: " & ex.Message, MsgBoxStyle.Exclamation, "هەڵەی #06")
        End Try

    End Sub

    Private Sub TxtR2_TextChanged(sender As Object, e As EventArgs) Handles TxtR2.TextChanged

    End Sub
    Private Sub TxtR1_TextChanged(sender As Object, e As EventArgs) Handles TxtR1.TextChanged

    End Sub


End Class

